package org.mozhu.mboot.admin.maintain.email.web.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.mozhu.mboot.admin.maintain.email.entity.EmailNotification;
import org.mozhu.mboot.admin.maintain.email.service.EmailNotificationService;
import org.mozhu.mboot.admin.maintain.notification.entity.NotificationModule;
import org.mozhu.mboot.admin.system.account.entity.User;
import org.mozhu.mboot.admin.system.account.service.UserService;
import org.mozhu.mboot.core.data.Searchable;
import org.mozhu.mboot.core.data.entity.Stateable;
import org.mozhu.mboot.core.util.BeanUtils;
import org.mozhu.mboot.core.web.bind.annotation.ControllerDefaults;
import org.mozhu.mboot.core.web.bind.annotation.SearchableDefaults;
import org.mozhu.mboot.core.web.controller.BaseCRUDController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RequestMapping("/maintain/email/notification")
@Controller
@ControllerDefaults(resourceIdentity = "maintain:emailNotification")
public class EmailNotificationController extends BaseCRUDController<EmailNotification, Long> {

    @Autowired
    UserService userService;

    protected EmailNotificationService getBaseService() {
        return (EmailNotificationService) baseService;
    }

    @Override
    protected void setCommonData(Model model) {
        super.setCommonData(model);
        model.addAttribute("modules", NotificationModule.values());
        model.addAttribute("processStatuses", Stateable.ProcessStatus.values());
    }

    @Override
    @SearchableDefaults(value = "EQ_status=pending")
    public String list(HttpServletRequest request, Searchable searchable, Model model, @RequestParam(value = "view", required = false, defaultValue = "list") String view) {
        String statusName = searchable.getValue("EQ_status");
        model.addAttribute("status", Stateable.ProcessStatus.valueOf(statusName));
        return super.list(request, searchable, model, view);
    }

    @Override
    @SearchableDefaults(value = "EQ_status=pending")
    public Object list(HttpServletRequest request, Searchable searchable) {
        return super.list(request, searchable);
    }

    @Override
    protected Map<String, Object> getExtraData(HttpServletRequest request, Page<EmailNotification> page) {
        Map<String, Object> extraDataMap = Maps.newHashMap();
        List<EmailNotification> authList = page.getContent();
        extraDataMap.put("users", getUsers(authList));
        return extraDataMap;
    }

    private Object getUsers(List<EmailNotification> notificationList) {
        Set<String> emails = notificationList.stream().map(a -> a.getEmail()).collect(Collectors.toSet());
        return findByEmails(emails).stream().collect(Collectors.toMap(User::getEmail, c -> BeanUtils.transBean2Map(c, "id", "name", "email")));
    }

    @Override
    protected void setExtraData(EmailNotification notification, Model model) {
        model.addAttribute("mUser", userService.findByEmail(notification.getEmail()));
    }

    public List<User> findByEmails(Collection<String> emails) {
        if (CollectionUtils.isEmpty(emails)) {
            return Lists.newArrayList();
        }
        List<User> users = Lists.newArrayList();
        for (String email : emails) {
            User user = userService.findByEmail(email);
            if (user != null) {
                users.add(user);
            }
        }
        return users;
    }
}